<template>
  <!-- 侧边栏 -->
  <div class="sidebar">
    <!-- 系统图标及名称 -->
    <div class="hospital-brand">
      <div class="hospital-icon">
        <img :src="logoUrl" alt="">
      </div>
      <div class="brand-text">智医云联</div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <router-link
        v-for="item in flatMenuItems"
        :to="item.path"
        :key="item.key"
        custom
        v-slot="{ navigate, isActive }"
      >
        <div class="nav-item" :class="{ active: isActive }" @click="navigate">
          <div class="nav-icon">
            <el-icon><component :is="item.icon" /></el-icon>
          </div>
          <div class="nav-text">{{ item.name }}</div>
        </div>
      </router-link>
      
      <!-- 人员管理折叠菜单 -->
      <div class="nav-item collapsible" :class="{ active: isPersonnelActive }" @click="togglePersonnelMenu">
        <div class="nav-icon">
          <el-icon><UserFilled /></el-icon>
        </div>
        <div class="nav-text">人员管理</div>
        <div class="nav-arrow" :class="{ rotated: personnelMenuOpen }">
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>
      
      <!-- 人员管理子菜单 -->
      <div class="sub-menu" :class="{ open: personnelMenuOpen }">
        <router-link
          v-for="item in personnelSubItems"
          :to="item.path"
          :key="item.key"
          custom
          v-slot="{ navigate, isActive }"
        >
          <div class="sub-nav-item" :class="{ active: isActive }" @click="navigate">
            <div class="sub-nav-icon">
              <el-icon><component :is="item.icon" /></el-icon>
            </div>
            <div class="sub-nav-text">{{ item.name }}</div>
          </div>
        </router-link>
      </div>
      
      <!-- 其他菜单项 -->
      <router-link
        v-for="item in otherMenuItems"
        :to="item.path"
        :key="item.key"
        custom
        v-slot="{ navigate, isActive }"
      >
        <div class="nav-item" :class="{ active: isActive }" @click="navigate">
          <div class="nav-icon">
            <el-icon><component :is="item.icon" /></el-icon>
          </div>
          <div class="nav-text">{{ item.name }}</div>
        </div>
      </router-link>
    </div>
    <!-- 用户信息及操作 -->
    <div class="user-profile">
      <el-dropdown trigger="click" @command="handleCommand">
        <div class="user-avatar">
          <el-icon><User /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>个人资料</el-dropdown-item>
            <el-dropdown-item>账户设置</el-dropdown-item>
            <el-dropdown-item divided @click="logout"
              >注销登录</el-dropdown-item
            >
          </el-dropdown-menu>
        </template>
      </el-dropdown>

      <div class="user-info">
        <div class="user-name">{{ username }}</div>
        <div class="user-role">系统管理员</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useStore } from "vuex";
import { 
  ElNotification,
  ElMessage,
  ElButton,
  ElIcon
} from 'element-plus';
import { UserFilled, ArrowRight } from '@element-plus/icons-vue';

const router = useRouter();
const route = useRoute();
const store = useStore();

// 人员菜单展开状态
const personnelMenuOpen = ref(false);
// logo图标
const logoUrl = require("@/assets/logo/logo_h.png");

// 配置菜单项 - 人员管理之前的菜单
const flatMenuItems = ref([
  { name: "控制台", path: "/manager/home", icon: "House", key: "dashboard" },
  {
    name: "科室管理",
    path: "/manager/department",
    icon: "OfficeBuilding",
    key: "department",
  },
]);

// 人员管理子菜单项
const personnelSubItems = ref([
  { name: "医生管理", path: "/manager/doctor", icon: "FirstAidKit", key: "doctor" },
  { name: "护士管理", path: "/manager/nurse", icon: "FirstAidKit", key: "nurse" },
  { name: "药剂师管理", path: "/manager/pharmacist", icon: "FirstAidKit", key: "pharmacist" },
  { name: "财务人员管理", path: "/manager/finance", icon: "User", key: "finance" },
  { name: "行政人员管理", path: "/manager/adminstaff", icon: "User", key: "adminstaff" },
  { name: "患者管理", path: "/manager/patient", icon: "User", key: "patient" },
]);

// 人员管理之后的菜单项
const otherMenuItems = ref([
  { name: "费用配置", path: "/manager/fee-configuration", icon: "Money", key: "fee-configuration" },
  { name: "排班模板管理", path: "/manager/schedule-templates", icon: "Calendar", key: "schedule" },
  { name: "排班实例管理", path: "/manager/schedule-instances", icon: "Document", key: "schedule-instances" },
  { name: "收费管理", path: "/manager/billing", icon: "Coin", key: "billing" },
  { name: "药品管理", path: "/manager/medicine", icon: "Box", key: "medicine" },
  { name: "设备管理", path: "/manager/equipment", icon: "Monitor", key: "equipment" },
  {
    name: "数据统计",
    path: "/manager/statistics",
    icon: "DataAnalysis",
    key: "statistics",
  },
  { name: "系统设置", path: "/manager/settings", icon: "Setting", key: "settings" },
]);

// 检查当前路由是否在人员管理子菜单中
const isPersonnelActive = computed(() => {
  return personnelSubItems.value.some(item => route.path.startsWith(item.path));
});

// 切换人员管理菜单展开状态
const togglePersonnelMenu = () => {
  personnelMenuOpen.value = !personnelMenuOpen.value;
};

// 初始化时，如果当前路由在人员管理子菜单中，自动展开
onMounted(() => {
  if (isPersonnelActive.value) {
    personnelMenuOpen.value = true;
  }
});

const username = ref(store.state.user?.username || "管理员");

const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      // 处理个人资料点击
      router.push('/doctor/profile');
      break;
    case 'settings':
      // 处理账户设置点击
      router.push('/doctor/settings');
      break;
    default:
      // 其他情况
      break;
  }
};

const logout = () => {
  // 清除本地存储的token
  localStorage.clear();
  // 重定向到登录页面
  router.push("/login");
  ElMessage.success("已安全退出系统");
};
</script>

<style lang="scss" scoped>
//设置本地字体
@font-face {
  font-family: 'MyBrushFont';
  src: url('@/assets/fonts/W04.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

/* 侧边栏样式 */
.sidebar {
  width: 250px;
  background: linear-gradient(135deg, #1a75bc 0%, #0c4d8c 100%);
  color: white;
  padding: 20px 0;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
}

.hospital-brand {
  display: flex;
  align-items: center;
  padding: 0 20px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  margin-bottom: 20px;
}

.hospital-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;

  img {
    width: 50px;
    height: 50px;
  }
}

.brand-text {
  font-family: 'MyBrushFont', 'ZCOOL XiaoWei', serif;
  font-size: 22px;  
  cursor: pointer;
}

.nav-menu {
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 14px 20px;
  cursor: pointer;
  transition: all 0.3s;
  position: relative;

  .nav-text{
    flex: 1;
    font-size: 15px;
    font-weight: 500;
  }

  &:hover {
    background: rgba(255, 255, 255, 0.1);
  }

  &.active {
    background: rgba(255, 255, 255, 0.2);
    border-left: 4px solid white;
  }
  
  &.collapsible {
    justify-content: space-between;
  }
}

.nav-icon {
  margin-right: 12px;
  width: 20px;
  text-align: center;
}

.nav-arrow {
  transition: transform 0.3s;
  
  &.rotated {
    transform: rotate(90deg);
  }
}

/* 子菜单样式 */
.sub-menu {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
  
  &.open {
    max-height: 300px; /* 根据子项数量调整，每增加一个加50 */
  }
}

.sub-nav-item {
  display: flex;
  align-items: center;
  padding: 10px 20px 10px 50px;
  cursor: pointer;
  transition: all 0.3s;
  background: rgba(0, 0, 0, 0.1);

  &:hover {
    background: rgba(255, 255, 255, 0.05);
  }

  &.active {
    background: rgba(255, 255, 255, 0.15);
    border-left: 2px solid rgba(255, 255, 255, 0.5);
  }
}

.sub-nav-icon {
  margin-right: 10px;
  width: 16px;
  text-align: center;
  opacity: 0.8;
}

.sub-nav-text {
  font-size: 14px;
}

.user-profile {
  position: relative;
  padding: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-evenly;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  cursor: pointer;
}

.user-name {
  font-weight: 500;
  font-size: 14px;
}

.user-role {
  font-size: 12px;
  opacity: 0.8;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .user-profile {
    .logout-icon {
      display: none;
    }
  }

  .sidebar {
    width: 80px;
  }

  .brand-text,
  .nav-text,
  .user-info,
  .sub-nav-text,
  .nav-arrow {
    display: none;
  }

  .hospital-brand {
    justify-content: center;
    padding: 15px 0;
  }

  .nav-item {
    justify-content: center;
    padding: 16px 0;
    
    &.collapsible {
      justify-content: center;
    }
  }

  .nav-icon {
    margin-right: 0;
  }

  .user-profile {
    justify-content: center;
  }
  
  .sub-nav-item {
    padding-left: 0;
    justify-content: center;
  }
  
  .sub-nav-icon {
    margin-right: 0;
  }
}

@media (max-width: 768px) {
  .stats-cards {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    left: -80px;
    height: 100%;
    z-index: 1000;
    transition: left 0.3s;
  }

  .sidebar.open {
    left: 0;
  }

  .nav-item.collapsible {
    justify-content: center;
  }

  .menu-toggle {
    display: block !important;
  }
}

.menu-toggle {
  display: none;
  margin-right: 15px;
  cursor: pointer;
}
</style>